/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.model.ev;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.PropertyType;
import offset.nodes.client.veditor.view.ev.BooleanValueChooser;
import offset.nodes.client.veditor.view.ev.EnumeratedValueChooser;
import offset.nodes.client.veditor.model.DataModel;
import offset.nodes.client.veditor.model.VirtualBlock;
import offset.nodes.client.veditor.model.VirtualElement;
import offset.nodes.client.veditor.model.component.ComponentViewMapper;
import offset.nodes.client.veditor.view.ev.ReferenceFromTypeChooser;
import offset.nodes.client.virtual.model.jcr.nodetype.PropertyDefinition;

/**
 * A factory to create DataElementView elements. It is used to allow for easier substitution
 * in the future, e.g. by a Spring factory.
 */
public class DataElementViewFactory {

    static DataElementViewMapper[] mappers = new DataElementViewMapper[]{
        new PredefinedViewMapper(), new ComponentViewMapper(), new ReferenceViewMapper(), new NodeChooserViewMapper(),
        new EnumeratedValueViewMapper(), new BooleanViewMapper(), new SelectQueryViewMapper()
    };

    /**
     * Does this virtual element have a view associated to it?
     * 
     * @param context
     * @return
     */
    public static boolean hasElementView(VirtualElement v) {
        if (v == null)
            return false;

        for (DataElementViewMapper mapper : mappers) {
            if (mapper.hasView(v))
                return true;
        }

        return false;
    }

    /**
     * Return the DataElementView appropriate for the element.
     * 
     * @param v
     * @return
     */
    public static DataElementView getView(VirtualElement v) {
        if (v == null)
            return null;

        for (DataElementViewMapper mapper : mappers) {
            if (mapper.hasView(v))
                return mapper.getView(v);
        }

        return null;
    }
}
